home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / as / sprite / RCS / sparc.c,v < prev    next >
Encoding:
Text File  |  1991-08-27  |  27.7 KB  |  1,403 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    rab:1.2; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     90.06.28.15.24.37;  author rab;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     90.02.07.12.37.01;  author rab;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @Added some missing opcodes and fixed some misc bugs.
  27. @
  28. text
  29. @/* sparc.c -- Assemble for the SPARC
  30.    Copyright (C) 1989 Free Software Foundation, Inc.
  31.  
  32. This file is part of GAS, the GNU Assembler.
  33.  
  34. GAS is free software; you can redistribute it and/or modify
  35. it under the terms of the GNU General Public License as published by
  36. the Free Software Foundation; either version 1, or (at your option)
  37. any later version.
  38.  
  39. GAS is distributed in the hope that it will be useful,
  40. but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  42. GNU General Public License for more details.
  43.  
  44. You should have received a copy of the GNU General Public License
  45. along with GAS; see the file COPYING.  If not, write to
  46. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  47.  
  48.  
  49. #include <stdio.h>
  50. #include <ctype.h>
  51.  
  52. #include "sparc-opcode.h"
  53. #include "as.h"
  54. #include "frags.h"
  55. #include "struc-symbol.h"
  56. #include "flonum.h"
  57. #include "expr.h"
  58. #include "hash.h"
  59. #include "md.h"
  60. #include "sparc.h"
  61. #include "write.h"
  62. #include "read.h"
  63. #include "symbols.h"
  64.  
  65. void md_begin();
  66. void md_end();
  67. void md_number_to_chars();
  68. void md_assemble();
  69. char *md_atof();
  70. void md_convert_frag();
  71. void md_create_short_jump();
  72. void md_create_long_jump();
  73. int  md_estimate_size_before_relax();
  74. void md_number_to_imm();
  75. void md_number_to_disp();
  76. void md_number_to_field();
  77. void md_ri_to_chars();
  78. void emit_relocations();
  79. static void sparc_ip();
  80.  
  81. relax_typeS md_relax_table[] = { 0 };
  82.  
  83. /* handle of the OPCODE hash table */
  84. static struct hash_control *op_hash = NULL;
  85.  
  86. static void s_seg(), s_proc(), s_data1(), s_reserve(), s_common();
  87. extern void s_globl(), s_long(), s_short(), s_space(), cons();
  88.  
  89. pseudo_typeS
  90. md_pseudo_table[] = {
  91.     { "common",     s_common,   0 },
  92.     { "global",     s_globl,    0 },
  93.     { "half",       cons,       2 },
  94.     { "proc",       s_proc,     0 },
  95.     { "reserve",    s_reserve,  0 },
  96.     { "seg",        s_seg,      0 },
  97.     { "skip",       s_space,    0 },
  98.     { "word",       cons,       4 },
  99.     { NULL,         0,          0 },
  100. };
  101.  
  102. int md_short_jump_size = 4;
  103. int md_long_jump_size = 4;
  104. int omagic  =  (0x103 << 16) | OMAGIC;  /* Magic number for header */
  105.  
  106. /* This array holds the chars that always start a comment.  If the
  107.     pre-processor is disabled, these aren't very useful */
  108. char comment_chars[] = "!";    /* JF removed '|' from comment_chars */
  109.  
  110. /* This array holds the chars that only start a comment at the beginning of
  111.    a line.  If the line seems to have the form '# 123 filename'
  112.    .line and .file directives will appear in the pre-processed output */
  113. /* Note that input_file.c hand checks for '#' at the beginning of the
  114.    first line of the input file.  This is because the compiler outputs
  115.    #NO_APP at the beginning of its output. */
  116. /* Also note that '/*' will always start a comment */
  117. char line_comment_chars[] = "#";
  118.  
  119. /* Chars that can be used to separate mant from exp in floating point nums */
  120. char EXP_CHARS[] = "eE";
  121.  
  122. /* Chars that mean this number is a floating point constant */
  123. /* As in 0f12.456 */
  124. /* or    0d1.2345e12 */
  125. char FLT_CHARS[] = "rRsSfFdDxXpP";
  126.  
  127. /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
  128.    changed in read.c .  Ideally it shouldn't have to know about it at all,
  129.    but nothing is ideal around here.
  130.  */
  131. int size_reloc_info = sizeof(struct reloc_info_sparc);
  132.  
  133. static unsigned char octal[256];
  134. #define isoctal(c)  octal[c]
  135. static unsigned char toHex[256];
  136.  
  137. /*
  138.  *  anull bit - causes the branch delay slot instructions to not be executed 
  139.  */
  140. #define ANNUL       (1 << 29)
  141.  
  142. struct sparc_it {
  143.     char    *error;
  144.     unsigned long opcode;
  145.     struct nlist *nlistp;
  146.     expressionS exp;
  147.     int pcrel;
  148.     enum reloc_type reloc;
  149. } the_insn, set_insn;
  150.  
  151. #ifdef __STDC__
  152. static void print_insn(struct sparc_it *insn);
  153. static int getExpression(char *str);
  154. #else
  155. static void print_insn();
  156. static int getExpression();
  157. #endif
  158. static char *expr_end;
  159. static int special_case;
  160.  
  161. #define SPECIAL_CASE_SET    1
  162.  
  163. /*
  164.  * sort of like s_lcomm
  165.  *
  166.  */
  167. static void
  168. s_reserve()
  169. {
  170.     char *name;
  171.     char c;
  172.     char *p;
  173.     int temp;
  174.     symbolS *symbolP;
  175.  
  176.     name = input_line_pointer;
  177.     c = get_symbol_end();
  178.     p = input_line_pointer;
  179.     *p = c;
  180.     SKIP_WHITESPACE();
  181.     if ( * input_line_pointer != ',' ) {
  182.     as_warn("Expected comma after name");
  183.     ignore_rest_of_line();
  184.     return;
  185.     }
  186.     input_line_pointer ++;
  187.     if ((temp = get_absolute_expression()) < 0) {
  188.     as_warn("BSS length (%d.) <0! Ignored.", temp);
  189.     ignore_rest_of_line();
  190.     return;
  191.     }
  192.     *p = 0;
  193.     symbolP = symbol_find_or_make(name);
  194.     *p = c;
  195.     if (strncmp(input_line_pointer, ",\"bss\"", 6) != 0) {
  196.     as_warn("bad .reserve segment: `%s'", input_line_pointer);
  197.     return;
  198.     }
  199.     input_line_pointer += 6;
  200.     if (symbolP->sy_other == 0 
  201.         && symbolP->sy_desc  == 0
  202.     && ((symbolP->sy_type  == N_BSS
  203.     && symbolP->sy_value == local_bss_counter)
  204.     || ((symbolP->sy_type & N_TYPE) == N_UNDF
  205.     && symbolP->sy_value == 0))) {
  206.         symbolP->sy_value = local_bss_counter;
  207.         symbolP->sy_type  = N_BSS;
  208.         symbolP->sy_frag  = & bss_address_frag;
  209.         local_bss_counter += temp;
  210.     } else {
  211.     as_warn( "Ignoring attempt to re-define symbol from %d. to %d.",
  212.         symbolP->sy_value, local_bss_counter );
  213.     }
  214.     demand_empty_rest_of_line();
  215.     return;
  216. }
  217.  
  218. static void
  219. s_common()
  220. {
  221.     register char *name;
  222.     register char c;
  223.     register char *p;
  224.     register int temp;
  225.     register symbolS *    symbolP;
  226.  
  227.     name = input_line_pointer;
  228.     c = get_symbol_end();
  229.     /* just after name is now '\0' */
  230.     p = input_line_pointer;
  231.     *p = c;
  232.     SKIP_WHITESPACE();
  233.     if ( * input_line_pointer != ',' ) {
  234.     as_warn("Expected comma after symbol-name");
  235.     ignore_rest_of_line();
  236.     return;
  237.     }
  238.     input_line_pointer ++; /* skip ',' */
  239.     if ( (temp = get_absolute_expression ()) < 0 ) {
  240.     as_warn(".COMMon length (%d.) <0! Ignored.", temp);
  241.     ignore_rest_of_line();
  242.     return;
  243.     }
  244.     *p = 0;
  245.     symbolP = symbol_find_or_make (name);
  246.     *p = c;
  247.     if (   (symbolP->sy_type & N_TYPE) != N_UNDF ||
  248.         symbolP->sy_other != 0 || symbolP->sy_desc != 0) {
  249.     as_warn( "Ignoring attempt to re-define symbol");
  250.     ignore_rest_of_line();
  251.     return;
  252.     }
  253.     if (symbolP->sy_value) {
  254.     if (symbolP->sy_value != temp) {
  255.         as_warn( "Length of .comm \"%s\" is already %d. Not changed to %d.",
  256.         symbolP->sy_name, symbolP->sy_value, temp);
  257.     }
  258.     } else {
  259.     symbolP->sy_value = temp;
  260.     symbolP->sy_type |= N_EXT;
  261.     }
  262.     know(symbolP->sy_frag == &zero_address_frag);
  263.     if (strncmp(input_line_pointer, ",\"bss\"", 6) != 0) {
  264.     p=input_line_pointer;
  265.     while(*p && *p!='\n')
  266.         p++;
  267.     c= *p;
  268.     *p='\0';
  269.     as_warn("bad .common segment: `%s'", input_line_pointer);
  270.     *p=c;
  271.     return;
  272.     }
  273.     input_line_pointer += 6;
  274.     demand_empty_rest_of_line();
  275.     return;
  276. }
  277.  
  278. static void
  279. s_seg()
  280. {
  281.  
  282.     if (strncmp(input_line_pointer, "\"text\"", 6) == 0) {
  283.     input_line_pointer += 6;
  284.     s_text();
  285.     return;
  286.     }
  287.     if (strncmp(input_line_pointer, "\"data\"", 6) == 0) {
  288.     input_line_pointer += 6;
  289.     s_data();
  290.     return;
  291.     }
  292.     if (strncmp(input_line_pointer, "\"data1\"", 7) == 0) {
  293.     input_line_pointer += 7;
  294.     s_data1();
  295.     return;
  296.     }
  297.     as_warn("Unknown segment type");
  298.     demand_empty_rest_of_line();
  299.     return;
  300. }
  301.  
  302. static void
  303. s_data1()
  304. {
  305.     subseg_new(SEG_DATA, 1);
  306.     demand_empty_rest_of_line();
  307.     return;
  308. }
  309.  
  310. static void
  311. s_proc()
  312. {
  313.     extern char is_end_of_line[];
  314.  
  315.     while (!is_end_of_line[*input_line_pointer]) {
  316.     ++input_line_pointer;
  317.     }
  318.     ++input_line_pointer;
  319.     return;
  320. }
  321.  
  322. static void
  323. s_sparc_align()
  324. {
  325.     register unsigned int temp;
  326.     register long int temp_fill;
  327.     unsigned int i;
  328.  
  329.     temp = get_absolute_expression ();
  330. #define MAX_ALIGNMENT (1 << 15)
  331.     if ( temp > MAX_ALIGNMENT ) {
  332.     as_warn("Alignment too large: %d. assumed.", temp = MAX_ALIGNMENT);
  333.     }
  334.  
  335.     /*
  336.      * For the sparc, `.align (1<<n)' actually means `.align n'
  337.      * so we have to convert it.
  338.      */
  339.     if (temp != 0) {
  340.     for (i = 0; (temp & 1) == 0; temp >>= 1, ++i)
  341.         ;
  342.     }
  343.     if (temp != 1) {
  344.     as_warn("Alignment not a power of 2");
  345.     }
  346.     temp = i;
  347.     if (*input_line_pointer == ',') {
  348.     input_line_pointer ++;
  349.     temp_fill = get_absolute_expression ();
  350.     } else {
  351.     temp_fill = 0;
  352.     }
  353.     /* Only make a frag if we HAVE to. . . */
  354.     if (temp && ! need_pass_2) {
  355.     frag_align (temp, (int)temp_fill);
  356.     }
  357.     demand_empty_rest_of_line();
  358.     return;
  359. }
  360.  
  361. /* This function is called once, at assembler startup time.  It should
  362.    set up all the tables, etc. that the MD part of the assembler will need.  */
  363. void
  364. md_begin()
  365. {
  366.   register char *retval = NULL;
  367.   int lose = 0;
  368.   register unsigned int i = 0;
  369.  
  370.   op_hash = hash_new();
  371.   if (op_hash == NULL)
  372.     as_fatal("Virtual memory exhausted");
  373.  
  374.   while (i < NUMOPCODES)
  375.     {
  376.       const char *name = sparc_opcodes[i].name;
  377.       retval = hash_insert(op_hash, name, &sparc_opcodes[i]);
  378.       if(retval != NULL && *retval != '\0')
  379.     {
  380.       fprintf (stderr, "internal error: can't hash `%s': %s\n",
  381.            sparc_opcodes[i].name, retval);
  382.       lose = 1;
  383.     }
  384.       do
  385.     {
  386.       if (sparc_opcodes[i].match & sparc_opcodes[i].lose)
  387.         {
  388.           fprintf (stderr, "internal error: losing opcode: `%s' \"%s\"\n",
  389.                sparc_opcodes[i].name, sparc_opcodes[i].args);
  390.           lose = 1;
  391.         }
  392.       ++i;
  393.     } while (i < NUMOPCODES
  394.          && !strcmp(sparc_opcodes[i].name, name));
  395.     }
  396.  
  397.   if (lose)
  398.     as_fatal ("Broken assembler.  No assembly attempted.");
  399.  
  400.   for (i = '0'; i < '8'; ++i)
  401.     octal[i] = 1;
  402.   for (i = '0'; i <= '9'; ++i)
  403.     toHex[i] = i - '0';
  404.   for (i = 'a'; i <= 'f'; ++i)
  405.     toHex[i] = i + 10 - 'a';
  406.   for (i = 'A'; i <= 'F'; ++i)
  407.     toHex[i] = i + 10 - 'A';
  408. }
  409.  
  410. void
  411. md_end()
  412. {
  413.     return;
  414. }
  415.  
  416. void
  417. md_assemble(str)
  418.     char *str;
  419. {
  420.     char *toP;
  421.     int rsd;
  422.  
  423.     assert(str);
  424.     sparc_ip(str);
  425.     toP = frag_more(4);
  426.     /* put out the opcode */
  427.     md_number_to_chars(toP, the_insn.opcode, 4);
  428.  
  429.     /* put out the symbol-dependent stuff */
  430.     if (the_insn.reloc != NO_RELOC) {
  431.     fix_new(
  432.         frag_now,                           /* which frag */
  433.         (toP - frag_now->fr_literal), /* where */
  434.         4,                                  /* size */
  435.         the_insn.exp.X_add_symbol,
  436.         the_insn.exp.X_subtract_symbol,
  437.         the_insn.exp.X_add_number,
  438.         the_insn.pcrel,
  439.         the_insn.reloc
  440.     );
  441.     }
  442.     switch (special_case) {
  443.  
  444.     case SPECIAL_CASE_SET:
  445.     special_case = 0;
  446.     assert(the_insn.reloc == RELOC_HI22);
  447.     toP = frag_more(4);
  448.     rsd = (the_insn.opcode >> 25) & 0x1f;
  449.     the_insn.opcode = 0x80102000 | (rsd << 25) | (rsd << 14);
  450.     md_number_to_chars(toP, the_insn.opcode, 4);
  451.     fix_new(
  452.         frag_now,                           /* which frag */
  453.         (toP - frag_now->fr_literal),       /* where */
  454.         4,                                  /* size */
  455.         the_insn.exp.X_add_symbol,
  456.         the_insn.exp.X_subtract_symbol,
  457.         the_insn.exp.X_add_number,
  458.         the_insn.pcrel,
  459.         RELOC_LO10
  460.     );
  461.     return;
  462.  
  463.     case 0:
  464.     return;
  465.  
  466.     default:
  467.     abort();
  468.     }
  469. }
  470.  
  471. static void
  472. sparc_ip(str)
  473.     char *str;
  474. {
  475.     char *s;
  476.     const char *args;
  477.     char c;
  478.     unsigned long i;
  479.     struct sparc_opcode *insn;
  480.     char *argsStart;
  481.     unsigned long   opcode;
  482.     unsigned int mask;
  483.     int match = FALSE;
  484.     int comma = 0;
  485.  
  486.     for (s = str; islower(*s) || (*s >= '0' && *s <= '3'); ++s)
  487.     ;
  488.     switch (*s) {
  489.  
  490.     case '\0':
  491.     break;
  492.  
  493.     case ',':
  494.     comma = 1;
  495.  
  496.     /*FALLTHROUGH*/
  497.  
  498.     case ' ':
  499.     *s++ = '\0';
  500.     break;
  501.  
  502.     default:
  503.         as_warn("Unknown opcode: `%s'", str);
  504.         exit(1);
  505.     }
  506.     if ((insn = (struct sparc_opcode *) hash_find(op_hash, str)) == NULL) {
  507.     as_warn("`%s' not in hash table.", str);
  508.     return;
  509.     }
  510.     if (comma) {
  511.     *--s = ',';
  512.     }
  513.     argsStart = s;
  514.     for (;;) {
  515.     opcode = insn->match;
  516.     bzero(&the_insn, sizeof(the_insn));
  517.     the_insn.reloc = NO_RELOC;
  518.  
  519.     /*
  520.      * Build the opcode, checking as we go to make
  521.      * sure that the operands match
  522.      */
  523.     for (args = insn->args; ; ++args) {
  524.         switch (*args) {
  525.  
  526.         case '\0':  /* end of args */
  527.         if (*s == '\0') {
  528.             match = TRUE;
  529.         }
  530.         break;
  531.  
  532.         case '+':
  533.         if (*s == '+') {
  534.             ++s;
  535.             continue;
  536.         }
  537.         if (*s == '-') {
  538.             continue;
  539.         }
  540.         break;
  541.  
  542.         case '[':   /* these must match exactly */
  543.         case ']':
  544.         case ',':
  545.         case ' ':
  546.         if (*s++ == *args)
  547.             continue;
  548.         break;
  549.  
  550.         case '#':   /* must be at least one digit */
  551.         if (isdigit(*s++)) {
  552.             while (isdigit(*s)) {
  553.             ++s;
  554.             }
  555.             continue;
  556.         }
  557.         break;
  558.  
  559.         case 'C':   /* coprocessor state register */
  560.         if (strncmp(s, "%csr", 4) == 0) {
  561.             s += 4;
  562.             continue;
  563.         }
  564.         break;
  565.  
  566.         case 'b':    /* next operand is a coprocessor register */
  567.         case 'c':
  568.         case 'D':
  569.             if (*s++ == '%' && *s++ == 'c' && isdigit(*s)) {
  570.             mask = *s++;
  571.             if (isdigit(*s)) {
  572.             mask = 10 * (mask - '0') + (*s++ - '0');
  573.             if (mask >= 32) {
  574.                 break;
  575.             }
  576.             } else {
  577.             mask -= '0';
  578.             }
  579.             switch (*args) {
  580.  
  581.             case 'b':
  582.             opcode |= mask << 14;
  583.             continue;
  584.  
  585.             case 'c':
  586.             opcode |= mask;
  587.             continue;
  588.  
  589.             case 'D':
  590.             opcode |= mask << 25;
  591.             continue;
  592.             }
  593.         }
  594.         break;
  595.  
  596.         case 'r':   /* next operand must be a register */
  597.         case '1':
  598.         case '2':
  599.         case 'd':
  600.         if (*s++ == '%') {
  601.             switch (c = *s++) {
  602.  
  603.             case 'f':   /* frame pointer */
  604.                 if (*s++ == 'p') {
  605.                 mask = 0x1e;
  606.                 break;
  607.             }
  608.             goto error;
  609.  
  610.             case 'g':   /* global register */
  611.             if (isoctal(c = *s++)) {
  612.                 mask = c - '0';
  613.                 break;
  614.             }
  615.             goto error;
  616.  
  617.             case 'i':   /* in register */
  618.             if (isoctal(c = *s++)) {
  619.                 mask = c - '0' + 24;
  620.                 break;
  621.             }
  622.             goto error;
  623.  
  624.             case 'l':   /* local register */
  625.             if (isoctal(c = *s++)) {
  626.                 mask= (c - '0' + 16) ;
  627.                 break;
  628.             }
  629.             goto error;
  630.  
  631.             case 'o':   /* out register */
  632.             if (isoctal(c = *s++)) {
  633.                 mask= (c - '0' + 8) ;
  634.                 break;
  635.             }
  636.             goto error;
  637.  
  638.             case 's':   /* stack pointer */
  639.                 if (*s++ == 'p') {
  640.                 mask= 0xe;
  641.                 break;
  642.             }
  643.             goto error;
  644.  
  645.             case 'r': /* any register */
  646.                 if (!isdigit(c = *s++)) {
  647.                 goto error;
  648.             }
  649.             /* FALLTHROUGH */
  650.             case '0': case '1': case '2': case '3': case '4':
  651.             case '5': case '6': case '7': case '8': case '9':
  652.             if (isdigit(*s)) {
  653.                 if ((c = 10 * (c - '0') + (*s++ - '0')) >= 32) {
  654.                 goto error;
  655.                 }
  656.             } else {
  657.                 c -= '0';
  658.             }
  659.             mask= c;
  660.             break;
  661.  
  662.             default:
  663.             goto error;
  664.             }
  665.             /*
  666.              * Got the register, now figure out where
  667.              * it goes in the opcode.
  668.              */
  669.             switch (*args) {
  670.  
  671.             case '1':
  672.             opcode |= mask << 14;
  673.             continue;
  674.  
  675.             case '2':
  676.             opcode |= mask;
  677.             continue;
  678.  
  679.             case 'd':
  680.             opcode |= mask << 25;
  681.             continue;
  682.  
  683.             case 'r':
  684.             opcode |= (mask << 25) | (mask << 14);
  685.             continue;
  686.             }
  687.         }
  688.         break;
  689.  
  690.         case 'e':    /* next operand is a floating point register */
  691.         case 'f':
  692.         case 'g':
  693.             if (*s++ == '%' && *s++ == 'f' && isdigit(*s)) {
  694.             mask = *s++;
  695.             if (isdigit(*s)) {
  696.             mask = 10 * (mask - '0') + (*s++ - '0');
  697.             if (mask >= 32) {
  698.                 break;
  699.             }
  700.             } else {
  701.             mask -= '0';
  702.             }
  703.             switch (*args) {
  704.  
  705.             case 'e':
  706.             opcode |= mask << 14;
  707.             continue;
  708.  
  709.             case 'f':
  710.             opcode |= mask;
  711.             continue;
  712.  
  713.             case 'g':
  714.             opcode |= mask << 25;
  715.             continue;
  716.             }
  717.         }
  718.         break;
  719.  
  720.         case 'F':
  721.         if (strncmp(s, "%fsr", 4) == 0) {
  722.             s += 4;
  723.             continue;
  724.         }
  725.         break;
  726.  
  727.         case 'h':       /* high 22 bits */
  728.             the_insn.reloc = RELOC_HI22;
  729.         goto immediate;
  730.  
  731.         case 'l':   /* 22 bit PC relative immediate */
  732.             the_insn.reloc = RELOC_WDISP22;
  733.         the_insn.pcrel = 1;
  734.         goto immediate;
  735.  
  736.         case 'L':   /* 30 bit immediate */
  737.             the_insn.reloc = RELOC_WDISP30;
  738.         the_insn.pcrel = 1;
  739.         goto immediate;
  740.  
  741.         case 'i':   /* 13 bit immediate */
  742.             the_insn.reloc = RELOC_BASE13;
  743.  
  744.         /*FALLTHROUGH*/
  745.  
  746.         immediate:
  747.         if(*s==' ')
  748.           s++;
  749.         if (*s == '%') {
  750.             if ((c = s[1]) == 'h' && s[2] == 'i') {
  751.             the_insn.reloc = RELOC_HI22;
  752.             s+=3;
  753.             } else if (c == 'l' && s[2] == 'o') {
  754.             the_insn.reloc = RELOC_LO10;
  755.             s+=3;
  756.             } else
  757.                 break;
  758.         }
  759.         /* Note that if the getExpression() fails, we will still have
  760.            created U entries in the symbol table for the 'symbols'
  761.            in the input string.  Try not to create U symbols for
  762.            registers, etc. */
  763.         {
  764.             /* This stuff checks to see if the expression ends
  765.                in +%reg If it does, it removes the register from
  766.                the expression, and re-sets 's' to point to the
  767.                right place */
  768.  
  769.             char *s1;
  770.  
  771.             for(s1=s;*s1 && *s1!=','&& *s1!=']';s1++)
  772.                 ;
  773.  
  774.             if(s1!=s && isdigit(s1[-1])) {
  775.                 if(s1[-2]=='%' && s1[-3]=='+') {
  776.                     s1-=3;
  777.                     *s1='\0';
  778.                     (void)getExpression(s);
  779.                     *s1='+';
  780.                     s=s1;
  781.                     continue;
  782.                 } else if(index("goli0123456789",s1[-2]) && s1[-3]=='%' && s1[-4]=='+') {
  783.                     s1-=4;
  784.                     *s1='\0';
  785.                     (void)getExpression(s);
  786.                     *s1='+';
  787.                     s=s1;
  788.                     continue;
  789.                 }
  790.             }
  791.         }
  792.         (void)getExpression(s);
  793.         s = expr_end;
  794.         continue;
  795.  
  796.         case 'a':
  797.         if (*s++ == 'a') {
  798.             opcode |= ANNUL;
  799.             continue;
  800.         }
  801.         break;
  802.  
  803.         case 'A':       /* alternate space */
  804.         if (isdigit(*s)) {
  805.             long num = 0;
  806.  
  807.             if (s[0] == '0' && s[1] == 'x') {
  808.             s += 2;
  809.             while (isxdigit(*s)) {
  810.                 if (isdigit(*s)) {
  811.                 num = num*16 + *s - '0';
  812.                 } else if (isupper(*s)) {
  813.                 num = num*16 + *s - 'A' + 10;
  814.                 } else {
  815.                 num = num*16 + *s - 'a' + 10;
  816.                 }
  817.                 ++s;
  818.             }
  819.             } else {
  820.             while (isdigit(*s)) {
  821.                 num= num*10 + *s-'0';
  822.                 ++s;
  823.             }
  824.             }
  825.             opcode |= num<<5;
  826.             continue;
  827.         }
  828.         break;
  829.         /* abort(); */
  830.  
  831.         case 'p':
  832.         if (strncmp(s, "%psr", 4) == 0) {
  833.             s += 4;
  834.             continue;
  835.         }
  836.         break;
  837.  
  838.         case 'q':   /* floating point queue */
  839.         if (strncmp(s, "%fq", 3) == 0) {
  840.             s += 3;
  841.             continue;
  842.         }
  843.         break;
  844.  
  845.         case 'Q':   /* coprocessor queue */
  846.         if (strncmp(s, "%cq", 3) == 0) {
  847.             s += 3;
  848.             continue;
  849.         }
  850.         break;
  851.  
  852.         case 'S':
  853.         if (strcmp(str, "set") == 0) {
  854.             special_case = SPECIAL_CASE_SET;
  855.             continue;
  856.         }
  857.         break;
  858.  
  859.         case 't':
  860.         if (strncmp(s, "%tbr", 4) != 0)
  861.             break;
  862.         s += 4;
  863.         continue;
  864.  
  865.         case 'w':
  866.         if (strncmp(s, "%wim", 4) != 0)
  867.             break;
  868.         s += 4;
  869.         continue;
  870.  
  871.         case 'y':
  872.         if (strncmp(s, "%y", 2) != 0)
  873.             break;
  874.         s += 2;
  875.         continue;
  876.  
  877.         default:
  878.         abort();
  879.         }
  880.         break;
  881.     }
  882.     error:
  883.     if (match == FALSE)
  884.       {
  885.         /* Args don't match.  */
  886.         if (&insn[1] - sparc_opcodes < NUMOPCODES
  887.         && !strcmp(insn->name, insn[1].name))
  888.           {
  889.         ++insn;
  890.         s = argsStart;
  891.         continue;
  892.           }
  893.         else
  894.           {
  895.         as_warn("Illegal operands");
  896.         return;
  897.           }
  898.       }
  899.     break;
  900.     }
  901.  
  902.     the_insn.opcode = opcode;
  903.     return;
  904. }
  905.  
  906. static int
  907. getExpression(str)
  908.     char *str;
  909. {
  910.     char *save_in;
  911.     segT seg;
  912.  
  913.     save_in = input_line_pointer;
  914.     input_line_pointer = str;
  915.     switch (seg = expression(&the_insn.exp)) {
  916.  
  917.     case SEG_ABSOLUTE:
  918.     case SEG_TEXT:
  919.     case SEG_DATA:
  920.     case SEG_BSS:
  921.     case SEG_UNKNOWN:
  922.     case SEG_DIFFERENCE:
  923.     case SEG_BIG:
  924.     case SEG_NONE:
  925.     break;
  926.  
  927.     default:
  928.     the_insn.error = "bad segment";
  929.     expr_end = input_line_pointer;
  930.     input_line_pointer=save_in;
  931.     return 1;
  932.     }
  933.     expr_end = input_line_pointer;
  934.     input_line_pointer = save_in;
  935.     return 0;
  936. }
  937.  
  938.  
  939. #define MAX_LITTLENUMS 6
  940.  
  941. /*
  942.     This is identical to the md_atof in m68k.c.  I think this is right,
  943.     but I'm not sure.
  944.  
  945.    Turn a string in input_line_pointer into a floating point constant of type
  946.    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
  947.    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
  948.  */
  949. char *
  950. md_atof(type,litP,sizeP)
  951.     char type;
  952.     char *litP;
  953.     int *sizeP;
  954. {
  955.     int    prec;
  956.     LITTLENUM_TYPE words[MAX_LITTLENUMS];
  957.     LITTLENUM_TYPE *wordP;
  958.     char    *t;
  959.     char    *atof_m68k();
  960.  
  961.     switch(type) {
  962.  
  963.     case 'f':
  964.     case 'F':
  965.     case 's':
  966.     case 'S':
  967.     prec = 2;
  968.     break;
  969.  
  970.     case 'd':
  971.     case 'D':
  972.     case 'r':
  973.     case 'R':
  974.     prec = 4;
  975.     break;
  976.  
  977.     case 'x':
  978.     case 'X':
  979.     prec = 6;
  980.     break;
  981.  
  982.     case 'p':
  983.     case 'P':
  984.     prec = 6;
  985.     break;
  986.  
  987.     default:
  988.     *sizeP=0;
  989.     return "Bad call to MD_ATOF()";
  990.     }
  991.     t=atof_m68k(input_line_pointer,type,words);
  992.     if(t)
  993.     input_line_pointer=t;
  994.     *sizeP=prec * sizeof(LITTLENUM_TYPE);
  995.     for(wordP=words;prec--;) {
  996.     md_number_to_chars(litP,(long)(*wordP++),sizeof(LITTLENUM_TYPE));
  997.     litP+=sizeof(LITTLENUM_TYPE);
  998.     }
  999.     return "";    /* Someone should teach Dean about null pointers */
  1000. }
  1001.  
  1002. /*
  1003.  * Write out big-endian.
  1004.  */
  1005. void
  1006. md_number_to_chars(buf,val,n)
  1007.     char *buf;
  1008.     long val;
  1009.     int n;
  1010. {
  1011.  
  1012.     switch(n) {
  1013.  
  1014.     case 4:
  1015.     *buf++ = val >> 24;
  1016.     *buf++ = val >> 16;
  1017.     case 2:
  1018.     *buf++ = val >> 8;
  1019.     case 1:
  1020.     *buf = val;
  1021.     break;
  1022.  
  1023.     default:
  1024.     abort();
  1025.     }
  1026.     return;
  1027. }
  1028.  
  1029. void
  1030. md_number_to_imm(buf,val,n, fixP, seg_type)
  1031.     char *buf;
  1032.     long val;
  1033.     int n;
  1034.     fixS *fixP;
  1035.     int seg_type;
  1036. {
  1037.     if (seg_type != N_TEXT || fixP->fx_r_type == (int) NO_RELOC) {
  1038.     switch (n) {
  1039.     case 1:
  1040.         *buf = val;
  1041.         break;
  1042.     case 2:
  1043.         *buf++ = (val>>8);
  1044.         *buf = val;
  1045.         break;
  1046.     case 4:
  1047.         *buf++ = (val>>24);
  1048.         *buf++ = (val>>16);
  1049.         *buf++ = (val>>8);
  1050.         *buf = val;
  1051.         break;
  1052.     default:
  1053.         abort();
  1054.     }
  1055.     return;
  1056.     }
  1057.  
  1058.     assert(n == 4);
  1059.     assert(fixP->fx_r_type < NO_RELOC);
  1060.  
  1061.     /*
  1062.      * This is a hack.  There should be a better way to
  1063.      * handle this.
  1064.      */
  1065.     if (fixP->fx_r_type == (int) RELOC_WDISP30 && fixP->fx_addsy) {
  1066.         val += fixP->fx_where + fixP->fx_frag->fr_address;
  1067.     }
  1068.  
  1069.     switch (fixP->fx_r_type) {
  1070.  
  1071.     case RELOC_32:
  1072.     buf[0] = val >> 24;
  1073.     buf[1] = val >> 16;
  1074.     buf[2] = val >> 8;
  1075.     buf[3] = val;
  1076.     break;
  1077.  
  1078. #if 0
  1079.     case RELOC_8:         /* These don't seem to ever be needed. */
  1080.     case RELOC_16:
  1081.     case RELOC_DISP8:
  1082.     case RELOC_DISP16:
  1083.     case RELOC_DISP32:
  1084. #endif
  1085.     case RELOC_WDISP30:
  1086.     val = (val >>= 2) + 1;
  1087.     buf[0] |= (val >> 24) & 0x3f;
  1088.     buf[1]= (val >> 16);
  1089.     buf[2] = val >> 8;
  1090.     buf[3] = val;
  1091.     break;
  1092.  
  1093.     case RELOC_HI22:
  1094.     if(!fixP->fx_addsy) {
  1095.       buf[1] |= (val >> 26) & 0x3f;
  1096.       buf[2] = val >> 18;
  1097.       buf[3] = val >> 10;
  1098.     } else {
  1099.       buf[2]=0;
  1100.       buf[3]=0;
  1101.     }
  1102.     break;
  1103. #if 0
  1104.     case RELOC_22:
  1105.     case RELOC_13:
  1106. #endif
  1107.     case RELOC_LO10:
  1108.     if(!fixP->fx_addsy) {
  1109.       buf[2] |= (val >> 8) & 0x03;
  1110.       buf[3] = val;
  1111.     } else
  1112.       buf[3]=0;
  1113.     break;
  1114. #if 0
  1115.     case RELOC_SFA_BASE:
  1116.     case RELOC_SFA_OFF13:
  1117.     case RELOC_BASE10:
  1118. #endif
  1119.     case RELOC_BASE13:
  1120.     buf[2] |= (val >> 8) & 0x1f;
  1121.     buf[3] = val;
  1122.     break;
  1123.  
  1124.     case RELOC_WDISP22:
  1125.     val = (val >>= 2) + 1;
  1126.     /* FALLTHROUGH */
  1127.     case RELOC_BASE22:
  1128.     buf[1] |= (val >> 16) & 0x3f;
  1129.     buf[2] = val >> 8;
  1130.     buf[3] = val;
  1131.     break;
  1132.  
  1133. #if 0
  1134.     case RELOC_PC10: 
  1135.     case RELOC_PC22: 
  1136.     case RELOC_JMP_TBL:
  1137.     case RELOC_SEGOFF16:
  1138.     case RELOC_GLOB_DAT:
  1139.     case RELOC_JMP_SLOT: 
  1140.     case RELOC_RELATIVE:
  1141. #endif
  1142.  
  1143.     case NO_RELOC:
  1144.     default:
  1145.     as_warn("bad relocation type: 0x%02x", fixP->fx_r_type);
  1146.     break;
  1147.     }
  1148.     return;
  1149. }
  1150.  
  1151. /* should never be called for sparc */
  1152. void
  1153. md_create_short_jump(ptr, from_addr, to_addr, frag, to_symbol)
  1154.     char *ptr;
  1155.     long from_addr, to_addr;
  1156. {
  1157.     fprintf(stderr, "sparc_create_short_jmp\n");
  1158.     abort();
  1159. }
  1160.  
  1161. /* should never be called for sparc */
  1162. void
  1163. md_number_to_disp(buf,val,n)
  1164.     char    *buf;
  1165.     long    val;
  1166. {
  1167.     fprintf(stderr, "md_number_to_disp\n");
  1168.     abort();
  1169. }
  1170.  
  1171. /* should never be called for sparc */
  1172. void
  1173. md_number_to_field(buf,val,fix)
  1174.     char *buf;
  1175.     long val;
  1176. #ifdef __STDC__
  1177.     void *fix;
  1178. #else
  1179.     char *fix;
  1180. #endif
  1181. {
  1182.     fprintf(stderr, "sparc_number_to_field\n");
  1183.     abort();
  1184. }
  1185.  
  1186. void
  1187. md_ri_to_chars(ri_p, ri)
  1188.     struct reloc_info_sparc *ri_p, ri;
  1189. {
  1190.     unsigned long n;
  1191.     char *p;
  1192.  
  1193.     p = (char *) ri_p;
  1194.     n = ri_p->r_address;
  1195.     p[3] = n;
  1196.     p[2] = n >> 8;
  1197.     p[1] = n >> 16;
  1198.     p[0] = n >> 24;
  1199.     n = (ri_p->r_index << 8) + (ri_p->r_extern << 7) + ((int) ri_p->r_type);
  1200.     p[7] = n;
  1201.     p[6] = n >> 8;
  1202.     p[5] = n >> 16;
  1203.     p[4] = n >> 24;
  1204.     n = ri_p->r_addend;
  1205.     p[11] = n;
  1206.     p[10] = n >> 8;
  1207.     p[9] = n >> 16;
  1208.     p[8] = n >> 24;
  1209.     return;
  1210. }
  1211.  
  1212. /* should never be called for sparc */
  1213. void
  1214. md_convert_frag(fragP)
  1215.     register fragS *fragP;
  1216. {
  1217.     fprintf(stderr, "sparc_convert_frag\n");
  1218.     abort();
  1219. }
  1220.  
  1221. /* should never be called for sparc */
  1222. void
  1223. md_create_long_jump(ptr, from_addr, to_addr, frag, to_symbol)
  1224.     char    *ptr;
  1225.     long    from_addr,
  1226.             to_addr;
  1227.     fragS    *frag;
  1228.     symbolS    *to_symbol;
  1229. {
  1230.     fprintf(stderr, "sparc_create_long_jump\n");
  1231.     abort();
  1232. }
  1233.  
  1234. /* should never be called for sparc */
  1235. int
  1236. md_estimate_size_before_relax(fragP, segtype)
  1237.     register fragS *fragP;
  1238. {
  1239.     fprintf(stderr, "sparc_estimate_size_before_relax\n");
  1240.     abort();
  1241.     return 0;
  1242. }
  1243.  
  1244. #if 0
  1245. /* for debugging only */
  1246. static void
  1247. print_insn(insn)
  1248.     struct sparc_it *insn;
  1249. {
  1250.     char *Reloc[] = {
  1251.     "RELOC_8",
  1252.     "RELOC_16",
  1253.     "RELOC_32",
  1254.     "RELOC_DISP8",
  1255.     "RELOC_DISP16",
  1256.     "RELOC_DISP32",
  1257.     "RELOC_WDISP30",
  1258.     "RELOC_WDISP22",
  1259.     "RELOC_HI22",
  1260.     "RELOC_22",
  1261.     "RELOC_13",
  1262.     "RELOC_LO10",
  1263.     "RELOC_SFA_BASE",
  1264.     "RELOC_SFA_OFF13",
  1265.     "RELOC_BASE10",
  1266.     "RELOC_BASE13",
  1267.     "RELOC_BASE22",
  1268.     "RELOC_PC10",
  1269.     "RELOC_PC22",
  1270.     "RELOC_JMP_TBL",
  1271.     "RELOC_SEGOFF16",
  1272.     "RELOC_GLOB_DAT",
  1273.     "RELOC_JMP_SLOT",
  1274.     "RELOC_RELATIVE",
  1275.     "NO_RELOC"
  1276.     };
  1277.  
  1278.     if (insn->error) {
  1279.     fprintf(stderr, "ERROR: %s\n");
  1280.     }
  1281.     fprintf(stderr, "opcode=0x%08x\n", insn->opcode);
  1282.     fprintf(stderr, "reloc = %s\n", Reloc[insn->reloc]);
  1283.     fprintf(stderr, "exp =  {\n");
  1284.     fprintf(stderr, "\t\tX_add_symbol = %s\n",
  1285.     insn->exp.X_add_symbol ?
  1286.     (insn->exp.X_add_symbol->sy_name ? 
  1287.     insn->exp.X_add_symbol->sy_name : "???") : "0");
  1288.     fprintf(stderr, "\t\tX_sub_symbol = %s\n",
  1289.     insn->exp.X_subtract_symbol ?
  1290.         (insn->exp.X_subtract_symbol->sy_name ? 
  1291.             insn->exp.X_subtract_symbol->sy_name : "???") : "0");
  1292.     fprintf(stderr, "\t\tX_add_number = %d\n",
  1293.     insn->exp.X_add_number);
  1294.     fprintf(stderr, "}\n");
  1295.     return;
  1296. }
  1297. #endif
  1298.  
  1299. /*
  1300.  * Sparc relocations are completely different, so it needs
  1301.  * this machine dependent routine to emit them.
  1302.  */
  1303. void
  1304. emit_relocations(fixP, segment_address_in_file)
  1305.     register fixS *fixP;
  1306.     relax_addressT segment_address_in_file;
  1307. {
  1308.     struct reloc_info_sparc ri;
  1309.     register symbolS *symbolP;
  1310.     extern char *next_object_file_charP;
  1311.     long add_number;
  1312.  
  1313.     bzero((char *) &ri, sizeof(ri));
  1314.     for (; fixP; fixP = fixP->fx_next) {
  1315.  
  1316.     if (fixP->fx_r_type >= (int) NO_RELOC) {
  1317.         fprintf(stderr, "fixP->fx_r_type = %d\n", fixP->fx_r_type);
  1318.         abort();
  1319.     }
  1320.  
  1321.     if ((symbolP = fixP->fx_addsy) != NULL) {
  1322.         ri.r_address = fixP->fx_frag->fr_address +
  1323.             fixP->fx_where - segment_address_in_file;
  1324.         if ((symbolP->sy_type & N_TYPE) == N_UNDF) {
  1325.         ri.r_extern = 1;
  1326.         ri.r_index = symbolP->sy_number;
  1327.         } else {
  1328.         ri.r_extern = 0;
  1329.         ri.r_index = symbolP->sy_type & N_TYPE;
  1330.         }
  1331.         if (symbolP && symbolP->sy_frag) {
  1332.         ri.r_addend = symbolP->sy_frag->fr_address;
  1333.         }
  1334.         ri.r_type = (enum reloc_type) fixP->fx_r_type;
  1335.         if (fixP->fx_pcrel) {
  1336. /*        ri.r_addend -= fixP->fx_where;          */
  1337.         ri.r_addend -= ri.r_address;            
  1338.         } else {
  1339.         ri.r_addend = fixP->fx_addnumber;
  1340.         }
  1341.         md_ri_to_chars((char *) &ri, ri);
  1342.         append(&next_object_file_charP, (char *)& ri, sizeof(ri));
  1343.     }
  1344.     }
  1345.     return;
  1346. }
  1347.  
  1348. int
  1349. md_parse_option(argP,cntP,vecP)
  1350.     char **argP;
  1351.     int *cntP;
  1352.     char ***vecP;
  1353. {
  1354.     return 1;
  1355. }
  1356.  
  1357. @
  1358.  
  1359.  
  1360. 1.1
  1361. log
  1362. @Initial revision
  1363. @
  1364. text
  1365. @d777 1
  1366. a777 1
  1367.             long num;
  1368. d779 17
  1369. a795 4
  1370.             num=0;
  1371.             while (isdigit(*s)) {
  1372.             num= num*10 + *s-'0';
  1373.             ++s;
  1374. d1009 1
  1375. a1009 1
  1376.     if (seg_type != N_TEXT || fixP->fx_r_type == NO_RELOC) {
  1377. d1037 1
  1378. a1037 1
  1379.     if (fixP->fx_r_type == RELOC_WDISP30 && fixP->fx_addsy) {
  1380. d1148 1
  1381. d1150 3
  1382. d1160 1
  1383. a1160 1
  1384.      struct reloc_info_sparc *ri_p, ri;
  1385. d1162 19
  1386. a1180 5
  1387.     /* 
  1388.      * if you want to cross-assemble from
  1389.      * a little-endian then you might need to
  1390.      * do some byte-swapping here.
  1391.      */
  1392. d1288 1
  1393. a1288 1
  1394.     if (fixP->fx_r_type >= NO_RELOC) {
  1395. d1306 1
  1396. a1306 1
  1397.         ri.r_type = fixP->fx_r_type;
  1398. d1313 1
  1399. a1313 2
  1400.  
  1401. /*        md_ri_to_chars((char *) &ri, ri);        */
  1402. @
  1403.